package com.gionee.survey.controller;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gionee.survey.bean.Answer;
import com.gionee.survey.bean.Question;
import com.gionee.survey.bean.Survey;
import com.gionee.survey.service.IAnswerService;
import com.gionee.survey.service.IQuestionService;
import com.gionee.survey.service.ISurveyService;

/**
 * 答案控制类
 * @author yuwei
 * @time 2017年4月12日 下午7:22:33
 */
@Controller
@RequestMapping("/answer")
public class AnswerController {
	@Resource
	private IAnswerService answerService;
	@Resource
	private IQuestionService questionService;
	@Resource
	private ISurveyService surveyService;
	/**
	 * 保存问卷
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/showAnswer")
	public String queryAnswer(HttpServletRequest request, HttpServletResponse response,Model model) throws IOException{
		String surveyType = request.getParameter("type");	// 获取传过来的类型
		String id = request.getParameter("obj");	// 获取传过来的json字符串
		String[] ja = id.split(";");	// 将json字符串数组拆分为单个json字符串
		/*
		 * 问卷
		 */
		String orgTitle = request.getParameter("orgTitle");
		Survey survey = new Survey();	// 问卷对象
		String survey_id = UUID.randomUUID().toString().replace("-", "");	// 问卷id
		survey.setId(survey_id);	// 
		survey.setTitle(orgTitle);
		if (surveyType != null && !"".equals(surveyType)) {
			survey.setType(surveyType);
		}
		survey.setCreateTime(new Date());
		surveyService.insert(survey);
		for (int i = 0; i < ja.length; i++) {
			JSONObject jb = JSON.parseObject(ja[i]);
			Question question = new Question();	// 问题对象
			Answer answer = new Answer();	// 答案对象
			/*
			 * 问题
			 */
			String type = jb.getString("id");
			String title = jb.getString("title");
			String question_id = UUID.randomUUID().toString().replace("-", "");	// 问题id 
			question.setId(question_id);
			question.setSurveyId(survey_id);
			question.setQuestionTitle(title);
			question.setQuestionType(type);
			question.setQuestionSort(i);
			questionService.insert(question);	// 插入数据库
			/*
			 * 答案
			 */
			for (int j = 1; j < jb.size() - 1; j++) {
				String as = jb.getString("answer_" + j);
				String answer_id = UUID.randomUUID().toString().replace("-", "");	// 答案id
				answer.setId(answer_id);
				answer.setAnswer(as);
				answer.setQuestionId(question_id);
				answer.setAnswerSort(j);
				answerService.insert(answer);	// 插入数据库
			}
		}
		String str = JSON.parse("{'success':'" + survey_id + "'}").toString();
		response.getWriter().print(str);
		response.getWriter().flush();
		response.getWriter().close();
		return "success";
	}
}
